"use strict";
cc._RF.push(module, '88e848sc/BHGosE6lscEMZL', 'main');
// scripts/main.ts

"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
var common = require("./common");
var _a = cc._decorator, ccclass = _a.ccclass, property = _a.property;
var log = console.log;
var main = /** @class */ (function (_super) {
    __extends(main, _super);
    function main() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    main.prototype.transmitReceive = function (signInfo, url) {
        try {
            var socket = new WebSocket('ws:' + url);
            socket.onopen = function (e) {
                socket.send(JSON.stringify(signInfo));
            };
        }
        catch (e) {
            return new Promise(function (res, rej) {
                res(null);
            });
        }
        return new Promise(function (res, rej) {
            socket.onmessage = function (e) {
                res(JSON.parse(e.data));
            };
        });
    };
    main.prototype.start = function () {
        var _this = this;
        this.node.getChildByName('logUsr').on(cc.Node.EventType.TOUCH_END, function (res) {
            var uid = _this.node.getChildByName('uid').getComponent(cc.EditBox).string;
            var roomId = _this.node.getChildByName('roomBox').getComponent(cc.EditBox).string;
            var ip = _this.node.getChildByName('ipBox').getComponent(cc.EditBox).string;
            if (uid.length == 6 && parseInt(uid).toString().length == 6 && roomId.length == 6 && parseInt(roomId).toString().length == 6) {
                _this.transmitReceive({
                    task: 'login',
                    data: { uid: uid }
                }, ip).then(function (data) {
                    if (data) {
                        common.setUid(uid);
                        common.setRoomId(roomId);
                        common.setUrl('ws:' + ip);
                        cc.director.loadScene("game");
                    }
                    else
                        common.alert("请填写正确信息", _this.node);
                });
            }
            else
                common.alert("请填写完整信息", _this.node);
        });
        this.node.getChildByName('signIn').on(cc.Node.EventType.TOUCH_END, function (touch) {
            var ip = _this.node.getChildByName('ipBox').getComponent(cc.EditBox).string;
            if (ip.length < 5)
                common.alert("请输入服务器地址", _this.node);
            else
                _this.signInWindow.active = true;
        });
        this.signInWindow.getChildByName('loginConfirm').on(cc.Node.EventType.TOUCH_END, function (e) {
            var uid = _this.signInWindow.getChildByName('idBox').getComponent(cc.EditBox).string;
            var name = _this.signInWindow.getChildByName('nameBox').getComponent(cc.EditBox).string;
            var ip = _this.node.getChildByName('ipBox').getComponent(cc.EditBox).string;
            if (uid.length == 6 && parseInt(uid).toString().length == 6) {
                _this.transmitReceive({
                    task: 'signIn',
                    data: { uid: uid, name: name }
                }, ip).then(function (data) {
                    common.alert("注册成功", _this.node);
                    _this.node.getChildByName('uid').getComponent(cc.EditBox).string = uid;
                });
            }
            _this.signInWindow.active = false;
        });
    };
    __decorate([
        property(cc.Node)
    ], main.prototype, "signInWindow", void 0);
    main = __decorate([
        ccclass
    ], main);
    return main;
}(cc.Component));
exports.default = main;

cc._RF.pop();